package com.sad.jetpack.v1.datamodel.api.extension.client.socket.ipc;
import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class DefaultIPCChatFileSession  implements IPCChatSession, IPCChatFileSessionListener.IFileDownloader,IPCChatFileSessionListener.IFileUploader {
    private IPCChatFileSessionListener fileActionListener;
    //private String downLoadTargetDir ="文件目录";
    private IPCChatSessionManager sessionManager;

    public IPCChatFileSessionListener getFileActionListener() {
        return fileActionListener;
    }

    public void setFileActionListener(IPCChatFileSessionListener fileActionListener) {
        this.fileActionListener = fileActionListener;
    }

    private OutputStream getFileOutStream(String fp) throws Exception{
        File f=new File(fp);
        if (!f.exists()){
            File d=f.getParentFile();
            if (!d.exists()){
                d.mkdirs();
            }
            f.createNewFile();
        }
        OutputStream outputStream=new FileOutputStream(f);
        return outputStream;
    }

    private InputStream getFileInStream(String fp) throws Exception{
        File f=new File(fp);
        if (f.exists()){
            InputStream inputStream=new FileInputStream(f);
            return inputStream;
        }
        return null;
    }

    @Override
    public void doDownLoad(int cacheSize,String fp) throws Exception{
        OutputStream fileOut=getFileOutStream(fp);
        sessionManager.getChatMessenger().syncReceive(cacheSize, new IPCStreamUtils.InputStreamReadingListener() {
            @Override
            public void onLoopReading(byte[] bytes, int readIndex,int futureSize) throws Exception {
                fileOut.write(bytes);
                if (fileActionListener !=null){
                    fileActionListener.onDownloadFileProgress(sessionManager,fp,readIndex,futureSize);
                }
            }
        });
        fileOut.flush();
        fileOut.close();
        if (fileActionListener !=null){
            fileActionListener.onReceivedFile(sessionManager,new File(fp));
        }
    }

    @Override
    public void doUpLoad(int cacheSize, String fp) throws Exception {
        InputStream fileIn=getFileInStream(fp);
        if (fileIn!=null){
            File f=new File(fp);
            sessionManager.getChatMessenger().syncSend(fileIn, cacheSize, (int) f.length(), false, new IPCStreamUtils.InputStreamReadingListener() {
                @Override
                public void onLoopReading(byte[] bytes, int readIndex, int futureSize) throws Exception {
                    if (fileActionListener !=null){
                        fileActionListener.onUploadFileProgress(sessionManager,fp,readIndex,futureSize);
                    }
                }
            });
            fileIn.close();
            if (fileActionListener!=null){
                fileActionListener.onSentFile(sessionManager,f);
            }
        }
    }

    @Override
    public void onReadyToStartLoopingChat(IPCChatSessionManager sessionManager, int cacheSize) {
        this.sessionManager=sessionManager;
        if (fileActionListener!=null){
            fileActionListener.onReadyToStart(sessionManager,this,this);
        }
    }

    @Override
    public void onLoopingChat(IPCChatSessionManager sessionManager, int cacheSize) throws Exception {
        //先接收文件名，才接收文件
        byte[] bs=sessionManager.getChatMessenger().syncReceive(cacheSize);//阻塞，如果想发送文件，可以在想发送文件的时候使用onReadyToStart方法里获得的uploader去发送。
        String fileName=new String(bs, StandardCharsets.UTF_8);
        if (fileActionListener!=null){
            fileActionListener.onChatFile(sessionManager,this,this,fileName);//接收到来自对方的文件信息，由外部决定如何操作
        }
        /*if(fileActionListener ==null) {
            doDownLoad(cacheSize, downLoadTargetDir +File.separator+fileName);
        }
        else {
            fileActionListener.onReadyToStart(sessionManager,this,this);
        }*/
    }

    @NonNull
    @Override
    public IPCChatDataType chatDataType() {
        return IPCChatDataType.FILE;
    }
}
